#include "ServerRenting.h"

vector<Server*> ServerRenting::m_ServerList;
vector<Server*> ServerRenting::m_SuperServerList;


ServerRenting::ServerRenting(void)
{
}

void ServerRenting::addServer(string servername,float processingspeed, float serviceprice)
{
	ServerRenting::m_ServerList.push_back(new Server(servername,processingspeed,serviceprice));
}

void ServerRenting::addSuperServer(string servername,float processingspeed, float serviceprice)
{
	ServerRenting::m_SuperServerList.push_back(new Server(servername,processingspeed,serviceprice));
}


void ServerRenting::initializeServers()
{
	if (ServerRenting::m_ServerList.empty() == true)
		{
			ServerRenting::addServer("ALPHA",250,12);
			ServerRenting::addServer("BETA",350,15);
			ServerRenting::addServer("GAMMA",500,20);
			ServerRenting::addServer("DELTA",650,25);
			ServerRenting::addServer("LAMBDA",800,30);
		}
}

void ServerRenting::initializeSuperServers()
{
	if(ServerRenting::m_SuperServerList.empty() == true)
		ServerRenting::addSuperServer("OMEGA",1500,50);
}




ResultInfo* ServerRenting::rentServer(User* userinfo)
{
	
	ServerRenting::initializeServers();
	int size = ServerRenting::m_ServerList.size();

	// Kiểm tra su ton tai cua server
	if(size == 0)
		return NULL; // khong co server nao tra ve NULL

	for(int i=0; i < size;i++)// Duyệt tìm server rảnh
	{
		bool busy = ServerRenting::m_ServerList[i]->checkBusy();// Kiem tra may ban

		if(busy == false)
		{
				ServerRenting::m_ServerList[i]->updateUserInfo(userinfo);// Lưu lại thông tin người dùng/

				float processingspeed = ServerRenting::m_ServerList[i]->getProcessingSpeed();

				float serviceprice = ServerRenting::m_ServerList[i]->getServicePrice();

				float difficulty = userinfo->getDifficulty();

				float processingtime = difficulty/(processingspeed*1000);

				float serverrent = processingtime * serviceprice;

				string servername = ServerRenting::m_ServerList[i]->getServerName();

				return new ResultInfo(servername, processingtime, serverrent);// Nếu rảnh trả thông tin server cho người dùng.
			
		}
	}

	return NULL;
}

ResultInfo* ServerRenting::rentSuperServer(User* userinfo)
{
	
	ServerRenting::initializeSuperServers();
	int size = ServerRenting::m_SuperServerList.size();

	// Kiểm tra su ton tai cua server
	if(size == 0)
		return NULL; // khong co server nao tra ve NULL

	for(int i=0; i < size;i++)// Duyệt tìm server rảnh
	{
		bool busy = ServerRenting::m_SuperServerList[i]->checkBusy();// Kiem tra may ban

		if(busy == false)
		{
			ServerRenting::m_SuperServerList[i]->updateUserInfo(userinfo);// Lưu lại thông tin người dùng/
			float processingspeed = ServerRenting::m_SuperServerList[i]->getProcessingSpeed();
			float serviceprice = ServerRenting::m_SuperServerList[i]->getServicePrice();
			float difficulty = userinfo->getDifficulty();
			float processingtime = difficulty/(processingspeed*1000);
			float serverrent = processingtime * serviceprice;
			string servername = ServerRenting::m_SuperServerList[i]->getServerName();

			return new ResultInfo(servername, processingtime, serverrent);// Nếu rảnh trả thông tin server cho người dùng.	
			
		}
	}

	return NULL;
}

void ServerRenting::destroyServers()
{
	int serversAmount = ServerRenting::m_ServerList.size();
	int superserversAmount = ServerRenting::m_SuperServerList.size();

	for(int i = 0 ; i< serversAmount; i++)
		delete ServerRenting::m_ServerList[i];// Huy cac con tro trong danh sach cac server

	ServerRenting::m_ServerList.clear();

	for(int i = 0 ; i< superserversAmount; i++)
		delete ServerRenting::m_SuperServerList[i]; // Huy cac con tro trong danh sach cac super server

	ServerRenting::m_SuperServerList.clear();

}




ServerRenting::~ServerRenting(void)
{
	
}
